package com.fattyCorps.game.util;

import com.fattyCorps.common.util.JwtUtils;
import com.fattyCorps.game.exception.game.TokenErrorException;

import javax.servlet.http.HttpServletRequest;

public class GameJwtUtils {
    public static final String PLAYER_ID_NAME = "player_id";
    private static final String JWT_SECRET = "vcf&*sgh$ffgame";

    public static String createPlayerToken(int id){
        return JwtUtils.createToken(PLAYER_ID_NAME, id, JWT_SECRET);
    }

    public static Integer getPlayerIdByRequest(HttpServletRequest request){
        return getPlayerIdByRequest(request, true);
    }

    public static Integer getPlayerIdByRequest(HttpServletRequest request, boolean t){
        Integer id = JwtUtils.getIdByRequest(request, GameJwtUtils.PLAYER_ID_NAME, JWT_SECRET);

        if (id == 0) {
            if (t) {
                throw new TokenErrorException();
            }
        }

        return id;
    }
}
